﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CLUBsInterpreter.AST;

namespace CLUBsInterpreter.ObjectTypes
{
	/**
	 * <summary>
	 * Describes an action object in the CLUBs environment.
	 * </summary>
	 **/
	class CLUBsAction : CLUBsObject
	{
		private IdentifierType m_returnType = IdentifierType.Unset;

		/**
		 * <summary>
		 * The parameter types required by this action, and the names used within the action.
		 * </summary>
		 **/
		public Dictionary<string, IdentifierType> ParameterTypes = new Dictionary<string, IdentifierType>();
		/**
		 * <summary>
		 * The return type of this action.
		 * </summary>
		 **/
		public IdentifierType ReturnType
		{
			get
			{
				return m_returnType;
			}
			set
			{
				m_returnType = value;
			}
		}

		// CONSTRUCTORS
		/**
		 * <summary>
		 * Default constructor creates an empty action.
		 * </summary>
		 **/
		public CLUBsAction()
		{
			// Set the object identifier type
			ObjectIdentifierType = IdentifierType.Action;
		}

		/**
		 * <see cref="CLUBsObject.CopyValueFromObject"/>
		 **/
        public override void CopyValueFromObject(CLUBsObject obj) //TODO: Action is a special boy
		{
			CLUBsAction actionObj = (CLUBsAction)obj;
            LocalVariables.Clear();

            foreach (KeyValuePair<string, CLUBsObject> var in actionObj.LocalVariables)
            {
                LocalVariables.Add(var.Key, var.Value);
            }
		}

		/**
		 * <summary>
		 * Constructor taking optional return type and dictionary of parameters, for instant action creation.
		 * </summary>
		 **/
		public CLUBsAction(IdentifierType returnType = IdentifierType.Unset, Dictionary<string,IdentifierType> paramTypes = null)
			: this()
		{
			// Set the return type
			ReturnType = returnType;
			// If any parameter types are specified, swap our dictionary with the one specified
			if (paramTypes != null)
				ParameterTypes = paramTypes;
		}

		public override string ToString()
		{
			string str = Name+"(";
			foreach (IdentifierType t in ParameterTypes.Values)
				str += t+", ";
			if (ParameterTypes.Count > 0) str = str.Substring(0, str.Length - 2);
			return str + ")";
		}

	}
}
